package com.nowcoder.community.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 */
@Component
@Slf4j
public class MailClient {

    @Autowired
    private JavaMailSender MailSender;
    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to,String subject,String context){
        MimeMessage message = MailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context,true);
            MailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            log.info("发送邮件失败力"+e.getMessage());
        }

    }

}
